package com.lkd.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.service.ESOrderService;
import com.lkd.vo.OrderVO;
import com.lkd.vo.Pager;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.time.LocalDate;
import java.util.ArrayList;

@Service
@Slf4j
public class ESOrderServiceImpl implements ESOrderService {

    @Autowired
    private RestHighLevelClient client;

    /*
    * 订单查询
    * */
    @Override
    public Pager<OrderVO> search(Integer pageIndex, Integer pageSize, String orderNo, String openId, LocalDate startDate, LocalDate endDate) throws Exception{
        //1. 创建searchRequest
        SearchRequest request = new SearchRequest("order");
        //2. 准备DSL
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        // 订单号
        if(StrUtil.isNotEmpty(orderNo)){
            boolQuery.must(QueryBuilders.termQuery("order_no",orderNo));
        }
        //微信用户id
        if(StrUtil.isNotEmpty(openId)){
            boolQuery.must(QueryBuilders.termQuery("open_id",openId));
        }
        //日期范围
        if(ObjectUtil.isNotEmpty(startDate) && ObjectUtil.isNotEmpty(endDate)){
            boolQuery.must(QueryBuilders.rangeQuery("update_time").gte(startDate).lte(endDate));
        }
        request.source().query(boolQuery);

        //2-2 分页
        request.source().from((pageIndex-1)*pageSize).size(pageSize);
        //2-3 排序
        request.source().sort("update_time",SortOrder.DESC);
        //3.发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4.解析响应
        SearchHits searchHits = response.getHits();
        //4.1 获取总记录数
        long total = searchHits.getTotalHits().value;
        //4.2 获取文档数组
        ArrayList<OrderVO> orderVoList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //获取文档source
            String json = hit.getSourceAsString();
            OrderVO orderVO = JSON.parseObject(json, OrderVO.class);
            orderVoList.add(orderVO);
        }
        Pager<OrderVO> pager = new Pager<>();
        pager.setPageIndex(pageIndex);
        pager.setPageSize(pageSize);
        pager.setTotalCount(total);
        pager.getTotalPage();//计算总页数
        pager.setCurrentPageRecords(orderVoList);
        return pager;


    }
}
